local skel = fk.CreateSkill {
  name = "rmt__zongjian",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["rmt__zongjian"] = "纵剑",
  [":rmt__zongjian"] = "锁定技，游戏开始时，所有角色获得<a href=':rmt__xuanjian'>〖玄剑〗</a>；你使用牌时令蓄势状态与你不同的角色无法响应；回合开始时，你可以获得<a href=':rmt__guijian'>〖归剑〗</a>，回合结束时，若你本回合未杀死过角色，失去之和〖纵剑〗。",

  ["#rmt__zongjian-gainskill"] = "纵剑：你可以获得“归剑”，本回合若未杀人则你失去之和〖纵剑〗！",

  ["$rmt__zongjian1"] = "满堂花醉三千客，一剑霜寒十四州。",
  ["$rmt__zongjian2"] = "龙吟涧底寒潭彻，剑在匣中作狂歌。",
}

local DIY = require "packages.diy_utility.diy_utility"

--- 判断蓄势状态不同
---@param player Player
---@param to Player
---@return boolean
local function compareReadySkillState(player, to)
  if player == to then return false end
  local states = {}
  for _, s in ipairs(player:getAllSkills()) do
    if s.visible and s:hasTag(DIY.ReadySkill) then
      table.insertIfNeed(states, DIY.isReadying(player, s))
    end
  end
  if #states == 0 then return false end
  local to_states = {}
  for _, s in ipairs(to:getAllSkills()) do
    if s.visible and s:hasTag(DIY.ReadySkill) then
      table.insertIfNeed(to_states, DIY.isReadying(to, s))
    end
  end
  if #to_states == 0 then return false end
  return #states > 1 or #to_states > 1 or states[1] ~= to_states[1]
end

skel:addEffect(fk.GameStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:doIndicate(player, room.alive_players)
    for _, p in ipairs(room:getAlivePlayers()) do
      room:handleAddLoseSkills(p, "rmt__xuanjian")
    end
  end,
})

skel:addEffect(fk.CardUsing, {
  mute = true, -- 还是手动放配音吧，有点吵
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      local tos = {}
      for _, to in ipairs(player.room.alive_players) do
        if compareReadySkillState(player, to) then
          table.insertIfNeed(tos, to)
        end
      end
      if #tos > 0 then
        event:setCostData(self, {tos = tos})
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local tos = event:getCostData(self).tos
    player.room:notifySkillInvoked(player, skel.name, "offensive", tos)
    if data.card.type == Card.TypeBasic or data.card:isCommonTrick() then -- 装备就不播语音了
      player:broadcastSkillInvoke(skel.name)
    end
    data.disresponsiveList =  data.disresponsiveList or {}
    table.insertTableIfNeed(data.disresponsiveList, tos)
  end,
})

skel:addEffect(fk.TurnStart, {
  anim_type = "big",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name)
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = self.name, prompt = "#rmt__zongjian-gainskill"})
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local skill
    for loop = 0, 30 do
      local name = loop == 0 and "rmt__guijian" or "rmt"..loop.."__guijian"
      if Fk.skills[name] and not player:hasSkill(name, true, true) then
        skill = name
        break
      end
    end
    if not skill then return end
    player.tag["rmt__zongjian_skill"] = skill
    room:handleAddLoseSkills(player, skill)
  end,
})

skel:addEffect(fk.TurnEnd, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player.tag["rmt__zongjian_skill"] ~= nil
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local skill = player.tag["rmt__zongjian_skill"]
    player.tag["rmt__zongjian_skill"] = nil
    if #player.room.logic:getEventsOfScope(GameEvent.Death, 1, function(e)
      local death = e.data
      return death.killer == player
    end, Player.HistoryTurn) == 0 then
      room:handleAddLoseSkills(player, "-"..skill)
      room:handleAddLoseSkills(player, "-"..skel.name)
    end
  end,
})

return skel
